package cn.ittaoli.szdz.controller;

import cn.ittaoli.szdz.model.CxMember;
import cn.ittaoli.szdz.model.DzJbxx;
import cn.ittaoli.szdz.pojo.JqGridResult;
import cn.ittaoli.szdz.service.IndexService;
import cn.ittaoli.szdz.service.SingleService;
import cn.ittaoli.utils.ExportExcel;
import cn.ittaoli.utils.FileUpload;
import com.github.pagehelper.PageInfo;
import com.google.code.kaptcha.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lub_lenovo on 2017/11/8.
 */
@Controller
@RequestMapping("/single")
public class SingleController {
    @Autowired
    private SingleService singleService;


    @RequestMapping("/index")
    public String login(){
        return "single/index";
    }

    @RequestMapping("/getData")
    @ResponseBody
    public JqGridResult getData(Integer page,Integer rows){
        PageInfo pageInfo=singleService.getData(page,rows);
        JqGridResult jqGridResult=new JqGridResult();

        jqGridResult.setPage(pageInfo.getPageNum());
        jqGridResult.setTotal(pageInfo.getTotal());
        jqGridResult.setRows(pageInfo.getList());
        jqGridResult.setRecords(pageInfo.getPageSize());

        return jqGridResult;
    }

    @RequestMapping("/addbscj")
    public String addbscj(){
        return "single/addbscj";
    }

    /**
     * 上传excel
     * @param file
     * @param request
     * @param model
     * @return
     * @throws IOException
     */
    @RequestMapping("/upexcel")
    public String upexcel(MultipartFile file,HttpServletRequest request,Model model) throws IOException {
        String path= FileUpload.upload(file,request);
        String absPath=request.getSession().getServletContext().getRealPath("")+path;
        Integer res=singleService.readExcel(absPath);
        if(res>0){
            return "redirect:/single/index";
        }else{
            model.addAttribute("msg","导入失败");
            return "index";
        }
    }


    @RequestMapping("/exportExcel")
    public String exportExcel(HttpServletResponse response) throws Exception {

        // 定义表的标题
        String title = "员工列表一览";
        //定义表的列名
        String[] rowsName = new String[] { "考生号", "姓名", "身份证号", "语文成绩", "数学成绩","外语成绩"};
        //定义表的内容
        List<Object[]> dataList = new ArrayList<Object[]>();

        Object[] objs = null;
        List<DzJbxx> dzJbxxes = singleService.selectAllJbxx();

        for (int i = 0; i < dzJbxxes.size(); i++) {
            DzJbxx per = dzJbxxes.get(i);
            objs = new Object[rowsName.length];
            objs[0] = per.getKsh();
            objs[1] = per.getXm();
            objs[2] = per.getSfzh();
            objs[3] = per.getYwcj();
            objs[4] = per.getSxcj();
            objs[5] = per.getWycj();
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//            String date = df.format(per.getJobtime());
//            objs[5] = date;
//            objs[6] = per.getResume();
//            objs[7] = per.getFilepath();
//            objs[8] = per.getDept().getDname();
            dataList.add(objs);
        }

        // 创建ExportExcel对象
        ExportExcel ex = new ExportExcel(title, rowsName, dataList);

        // 输出Excel文件
        try {
            OutputStream output = response.getOutputStream();
            response.reset();
            response.setHeader("Content-disposition",
                    "attachment; filename=student.xls");
            response.setContentType("application/msexcel");
            ex.export(output);
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "single/index";// 返回列表显示
    }
}
